package org.kms.Controllers;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.kms.dao.szdetailsDao;
import org.kms.pojo.*;
import org.kms.pojo.Dictionary;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/Szdetails")
@CrossOrigin(value = "*")
public class szdetails {

    @Autowired
    private szdetailsDao szdetailsDao;


    @RequestMapping("/all")
    @ResponseBody
    public ResponseResult<List<Szdetails>> all(@RequestParam(defaultValue = "1") int page, String cha, @RequestParam(defaultValue = "10") int limit, Szdetails condition,String chaUName) throws ParseException {
//        System.out.println(condition.getDidyj());
        if(chaUName != null){
            Dictionary d = new Dictionary();
            d.setDid(Integer.parseInt(chaUName));
            condition.setSzName(d);
        }
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-mm-dd");


        Page<Szdetails> pager = PageHelper.startPage(page,limit);
        List<Szdetails> list = szdetailsDao.getConditionList(condition);

        return new ResponseResult(list, pager.getTotal());
    }

    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<Szdetails>> list(@RequestParam(defaultValue = "1") int page, String cha, @RequestParam(defaultValue = "10") int limit, Szdetails condition,String chaUName) throws ParseException {

        if(chaUName != null){
            Dictionary d = new Dictionary();
            d.setDid(Integer.parseInt(chaUName));
            condition.setSzName(d);
        }
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-mm-dd");


        Page<Szdetails> pager = PageHelper.startPage(page,limit);
        List<Szdetails> list = szdetailsDao.getCondition(condition);

        return new ResponseResult(list, pager.getTotal());
    }


    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(Szdetails szdetails, String roleIds)throws Exception{
        szdetailsDao.add(szdetails);
        return ResponseResult.createSuccessResult();
    }


    @RequestMapping("/update")
    @ResponseBody
    private ResponseResult update(Szdetails szdetails,String roleIds)throws Exception{
        szdetails.setDidstate(13);
        szdetailsDao.update(szdetails);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/up")
    @ResponseBody
    private ResponseResult up(Szdetails szdetails,String roleIds)throws Exception{
        szdetails.setDidstate(14);
        szdetailsDao.update(szdetails);
        return ResponseResult.createSuccessResult();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(Integer id) throws Exception{
        szdetailsDao.delete(id);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public ResponseResult batchDelete(Integer[] id)throws Exception{
        szdetailsDao.batchDelete(id);
        return ResponseResult.createSuccessResult();
    }


    @InitBinder
    public void InitBinder(WebDataBinder webDataBinder) throws Exception{
        //日期类型转换器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat,true);
        //注册类型换换器:如果碰到Date.class 都会转成yyyy-MM-dd格式
        webDataBinder.registerCustomEditor(Date.class,dateEditor);
    }

}
